package com.example.block_chain.utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/*
 *@description: 计算merkel树节点hash值
 *@author: hisen
 *@Time: 2023/11/20  18:11
 */
public class SHAUtil {
    public static String getShA256BasedMD(String originalStr) {
        MessageDigest messageDigest;
        String encodeStr;
        try {
            messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(originalStr.getBytes(StandardCharsets.UTF_8));
            encodeStr = Hex.encodeHexString(hash);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return encodeStr;
    }

    public static String sha256BasedHexTool(String originalStr) {
        return DigestUtils.sha256Hex(originalStr);
    }
}